package com.zhoutao.blog.seven.config;


import com.zhoutao.blog.seven.model.Response;
import com.zhoutao.blog.seven.model.exception.BaseBizException;
import com.zhoutao.blog.seven.model.exception.ErrorCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public Response<String> globalException(HttpServletRequest request, Exception e) {
        if (e instanceof BaseBizException) {
            BaseBizException bizException = (BaseBizException) e;
            return Response.fail(bizException.code(), bizException.message());
        }
        return Response.fail(ErrorCode.SYSTEM_ERROR.getCode(), e.getMessage());

    }
}